Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

177
Views
Matriz JSON de objetos con propiedades "desde" y "hasta" y agrúpelo por período de tiempo

En caso de que las fechas en la matriz se crucen, la función debe combinarlas en un período, de lo contrario, los períodos deben estar separados

 var dates = [ { "from":"03/01/2021", "to":"03/05/2021" }, { "from":"03/08/2021", "to":"03/10/2021" }, { "from":"03/07/2021", "to":"03/20/2021" } ]; var item = dates; var index = 0; var out = document.querySelector('#test'); for(var i = 0; i < item.length; i++){ index++; var from_i = Date.parse(item[i].from); var to_i = Date.parse(item[i].to); var from_index = Date.parse(item[index].from); var to_index = Date.parse(item[index].to); if(from_index >= from_i && from_index <= to_i){ let updated_from = new Date(from_index); let update_to = new Date(to_index); console.log(updated_from); out.innerHTML += updated_from.toDateString() + update_to.toDateString() + "<br/>"; } }
 <div id="test"> </div>

Caso de jSON entrante: [ { "desde":"01/03/2021", "hasta":"06/03/2021" }, { "desde":"10/03/2021", "hasta":" 15/03/2021" }, { "desde":"20/03/2021", "hasta":"25/03/2021" } ]

Salida: 1-5 de marzo, 7-20 de marzo

¿Puede alguien ayudar? Aprender JS... un poco difícil para mí(

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

puedes hacer algo como esto

 const data = [ { "from":"03/01/2021", "to":"03/06/2021" }, { "from":"03/10/2021", "to":"03/15/2021" }, { "from":"03/20/2021", "to":"03/25/2021" } ] const monthToString = month => { const months = { '01': 'Jan', '02': 'Feb', '03': 'Mar', '04': 'Apr', '05': 'May', '06': 'Jun', '07': 'Jul', '08': 'Aug', '09': 'Sep', '10': 'Oct', '11': 'Nov', '12': 'Dec', } return months[month] || month } const result = Object.entries(data.reduce((res, {from, to}) => { const [monthFrom, day, year] = from.split('/') const [m, dayTo, y] = to.split('/') const month = monthToString(m) return { ...res, [month]: [...(res[month] || []), [day, dayTo]] } }, {})).map(([month, days]) => `${month} ${days.map(([from, to]) => `${parseInt(from)}-${parseInt(to)}`).join(', ')}`).join('\n') console.log(result)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!